package org.dreamfly.netshop.actions.pub;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ResultPath;
import org.dreamfly.netshop.entity.GoodsInfo;
import org.dreamfly.netshop.entity.Type;
import org.dreamfly.netshop.manage.ClassManager;
import org.dreamfly.netshop.manage.GoodsInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户管理Action. 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * @author calvin
 */
@Namespace("/")
@ResultPath("/pages/pub")
public class IndexAction extends ActionSupport {

    @Autowired
    private ClassManager classManager;

    @Autowired
    private GoodsInfoManager goodsInfoManager;

    // @Autowired
    // private MemberManager memberManager;

    // private Member member;

    private List<Type> allClass;

    private Page<GoodsInfo> goodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> reGoodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> hotGoodsInfo = new Page<GoodsInfo>(4);

    private Page<GoodsInfo> spGoodsInfo = new Page<GoodsInfo>(4);

    public List<Type> getAllClass() {
        return allClass;
    }

    public Page<GoodsInfo> getGoodsInfo() {
        return goodsInfo;
    }

    public Page<GoodsInfo> getHotGoodsInfo() {
        return hotGoodsInfo;
    }

    public Page<GoodsInfo> getSpGoodsInfo() {
        return spGoodsInfo;
    }

    public Page<GoodsInfo> getReGoodsInfo() {
        return reGoodsInfo;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        allClass = classManager.getAll();
        reGoodsInfo.setOrder("DESC");
        reGoodsInfo.setOrderBy("addDate");
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("refinement", true, MatchType.EQUAL));
        reGoodsInfo = goodsInfoManager.search(reGoodsInfo, list);
        hotGoodsInfo.setOrder("DESC");
        hotGoodsInfo.setOrderBy("addDate");
        list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("hot", true, MatchType.EQUAL));
        hotGoodsInfo = goodsInfoManager.search(hotGoodsInfo, list);
        spGoodsInfo.setOrder("DESC");
        spGoodsInfo.setOrderBy("addDate");
        list = new ArrayList<PropertyFilter>();
        list.add(new PropertyFilter("refinement", true, MatchType.EQUAL));
        spGoodsInfo = goodsInfoManager.search(spGoodsInfo, list);
        return SUCCESS;
    }
    // public static void main(String[] args) {
    // Session session = null;
    //
    // try {
    // SessionFactory sessions = new Configuration().configure().buildSessionFactory();
    //
    // session = sessions.openSession();
    // System.out.println("Inserting Record");
    // TbClass message = new TbClass();
    // message.setClassName("abc");
    // session.save(message);
    // System.out.println(message.getClassId());
    // session.connection().setAutoCommit(true);
    // // session.setFlushMode(FlushMode.AUTO/ FlushMode.COMMIT/FlushMode.NEVER/FlushMode.ALWAYS);
    //
    // session.flush();
    // session.close();
    // // tx.commit();
    // System.out.println("Done");
    // } catch (Exception e) {
    // System.out.println(e.getMessage());
    // }
    // }
}
